package com.bupt.controller;


import com.bupt.dao.DeviceChannelDirectionMapper;
import com.bupt.dao.MapRepository;
import com.bupt.service.MapService;
import com.bupt.vo.DeviceChannelDirectionL;
import com.bupt.vo.DeviceChannelDirectionVo;
import com.bupt.vo.PositionVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Api(value="主页地图信息接口",description="首页地图曲线、定时刷新分区报警信息")
@RestController
public class MapController {

    @Autowired
    private MapRepository mapRepository;

    @Autowired
    private MapService mapService;

    @Autowired
    DeviceChannelDirectionMapper deviceChannelDirectionMapper;

    @ApiOperation("首页地图曲线")
    @RequestMapping(value = "/longitudeAndlatitude",method = RequestMethod.GET)
    public List longitudeAndlatitude() {
        Integer sameChannelIndex=0;
        Integer sameDeviceChannelId=0;
        List<DeviceChannelDirectionVo> list = deviceChannelDirectionMapper.selectAllDeviceChannelDirection();
        List<DeviceChannelDirectionL> deviceChannelDirectionLS = new ArrayList<DeviceChannelDirectionL>();
        if(list.size()>0){
            DeviceChannelDirectionL deviceChannelDirectionLSame=new DeviceChannelDirectionL();
            for (int i = 0; i <list.size() ; i++) {
                DeviceChannelDirectionL deviceChannelDirectionL=new DeviceChannelDirectionL();
               if(sameChannelIndex==list.get(i).getChannelIndex()&&sameDeviceChannelId.intValue()==list.get(i).getDeviceChannelId().intValue()){
                    List<PositionVo> position = deviceChannelDirectionLSame.getPosition();
                    PositionVo positionVo = new PositionVo();
                    positionVo.setLatitude(list.get(i).getLatitude());
                    positionVo.setLongitude(list.get(i).getLongitude());
                    position.add(positionVo);
                    if(i==list.size()-1){
                        //deviceChannelDirectionLS.add(deviceChannelDirectionLSame);
                        break;
                    }
                    if(sameChannelIndex!=list.get(i+1).getChannelIndex()){
                        deviceChannelDirectionLSame=new DeviceChannelDirectionL();
                    }
                    continue;
                }

                deviceChannelDirectionL.setDeviceName(list.get(i).getDeviceName());
                deviceChannelDirectionL.setChannelIndex(list.get(i).getChannelIndex());
                deviceChannelDirectionL.setDeviceChannelId(list.get(i).getDeviceChannelId());
                PositionVo positionVo = new PositionVo();
                positionVo.setLatitude(list.get(i).getLatitude());
                positionVo.setLongitude(list.get(i).getLongitude());
                ArrayList<PositionVo> positionVos = new ArrayList<>();
                positionVos.add(positionVo);
                deviceChannelDirectionL.setPosition(positionVos);

                deviceChannelDirectionLS.add(deviceChannelDirectionL);
                deviceChannelDirectionLSame=deviceChannelDirectionL;
                sameChannelIndex=list.get(i).getChannelIndex();
                sameDeviceChannelId=list.get(i).getDeviceChannelId();
            }

        }


        return deviceChannelDirectionLS;
    }

    @ApiOperation("查询分区报警信息")
    @RequestMapping(value = "/areaWarning",method = RequestMethod.GET)
    public List areaWarning() {

        ArrayList list = new ArrayList<>();
        //查询到分区的经纬度和分区索引
        List<Map<String, Object>> areaInfo = mapRepository.findAreaWarning();
        //根据分区索引查询到最近的报警未处理信息
        for (int i = 0; i <areaInfo.size() ; i++) {
            Map<String, Object> areaMap = areaInfo.get(i);
            String section_index = areaMap.get("section_index").toString();
            List<Map<String, Object>> areaToEvent=new ArrayList<>();
             areaToEvent = mapRepository.getAreaToEvent(section_index);
            if(areaToEvent.size()!=0){
                for (int j = 0; j <areaToEvent.size() ; j++) {

                    Map<String, Object> stringObjectMap = areaToEvent.get(j);
                    list.add(stringObjectMap);
                }
            }else{
                //list.add(new HashMap<>());
            }
        }
        return list;
    }
}
